/**
 * 
 */
package com.gopay.checkorder.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.gopay.checkorder.service.BankOrderService;
import com.gopay.checkorder.service.task.BankCountDownLatch;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.NjcbBankStatus;
import com.gopay.common.constants.bank.NjcbContants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.dayend.BaseDayendEntry;
import com.gopay.common.domain.bank.njcb.ebank.query.request.NjcbeBankDataReq;
import com.gopay.common.domain.bank.njcb.ebank.query.request.OpReq;
import com.gopay.common.domain.bank.njcb.ebank.query.request.ReqParam;
import com.gopay.common.domain.bank.njcb.ebank.query.response.NjcbeBankDataRep;
import com.gopay.common.domain.cashier.BankCardType;
import com.gopay.common.domain.cps.vo.CheckOrderResult;
import com.gopay.common.domain.cps.vo.InAccInfo;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.BankInteractRemoteService;

/**
 * @CheckOrderPsbcB2c.java
 * @author Ryan
 * @2013-12-4 上午9:30:47  www.gopay.com.cn Inc.All rights reserved.
 */
public class CheckOrderNjcbB2cImpl implements BankOrderService {

	private Logger logger = Logger.getLogger("NJCB");
	@Resource(name = "bankClient")
	private BankInteractRemoteService bankClient;
	@Override
	public CheckOrderResult checkOrder(BankOrder bankOrder, String target) {
		CheckOrderResult checkOrderResult = new CheckOrderResult();
		checkOrderResult.setOrderId(bankOrder.getOrderId());
		NjcbeBankDataReq message = new NjcbeBankDataReq();
		message.setBankOrderId(bankOrder.getOutOrderId());
		message.setOrderId(bankOrder.getOrderId());
		message.setBank(BankConstants.BANK_LOGO_NJCB);
		OpReq opReq = new OpReq();
		ReqParam reqParam = new ReqParam();
		message.setBank(BankConstants.BANK_LOGO_NJCB);
		message.setBankBusProd(BisBankBusinessProd.get(bankOrder.getBankBizProdCode()));
		message.setBankIntrChann(BisBankInterfaceChannel.get(bankOrder.getInterfaceChennelCode()));
		reqParam.setOrderNo(bankOrder.getOrderId());
		String sendDateTime = DateUtils.format(bankOrder.getSendBankTm());
		reqParam.setStartDate(sendDateTime.substring(0, 8));
		reqParam.setEndDate(sendDateTime.substring(0, 8));
		opReq.setReqParam(reqParam);
		opReq.setReqTime(sendDateTime.substring(8));
		message.setOpReq(opReq);

		Result result = null;
		try {
			result = bankClient.query(message);
			if (result == null || !result.isSuccess()) {
				logger.error("bankClient fail.");
				if(result ==null || ProcCodeConstants.PROC_CODE_100E6037.equals(result.getResultCode())){
					BankCountDownLatch.failIncrease(bankOrder.getBankCode());
					checkOrderResult.setCommunicateError(true);
				}
				checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
				checkOrderResult.setErrMsg("系统返回对账结果为空");
				return checkOrderResult;
			}

			NjcbeBankDataRep res = (NjcbeBankDataRep) result.getDefaultModel();
			if (res == null) {
				// 查询订单为空
				logger.error("fail:orderId:" + bankOrder.getOrderId()+ " errcode:" + ProcCodeConstants.PROC_CODE_100E6022+ " err:bis query is null.");
				checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
				checkOrderResult.setErrMsg("银行返回对账结果为空");
				return checkOrderResult;
			}

			if (!NjcbContants.QUERY_ORDER_TRANS_SUCCESS.equals(res.getOpRep().getRetCode())) {
				// 查询订单错误
				logger.error("fail:orderId:" + bankOrder.getOrderId()+ " errcode:" + res.getOpRep().getRetCode() + " err:" + res.getOpRep().getErrMsg());
				checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E5062);
				checkOrderResult.setErrMsg(res.getOpRep().getErrMsg());
				return checkOrderResult;
			}
			//成功处理
			if (StringUtils.equals(NjcbContants.ORDER_TRANS_RESULT_SUCC,res.getOpRep().getOpResult().getOpResultSet().getTranResult())) {
				checkOrderResult.setSuccess(true);
				logger.error("南京银行B2C订单" + bankOrder.getOrderId() + "的交易"+ NjcbBankStatus.getName(res.getOpRep().getOpResult().getOpResultSet().getTranResult()));
				InAccInfo accInfo = new InAccInfo();
				accInfo.setOrderId(bankOrder.getOrderId());
				accInfo.setAmount(new BigDecimal(res.getOpRep().getOpResult().getOpResultSet().getOrderAmt()));
				accInfo.setRetCode("00");
				accInfo.setCardType(BankCardType.Unknown.toString());
				checkOrderResult.setAccInfo(accInfo);
			}
			//失败处理
			else if(StringUtils.equals(NjcbContants.ORDER_TRANS_RESULT_FAIL,res.getOpRep().getOpResult().getOpResultSet().getTranResult())){
				checkOrderResult.setBankFailure(true);
				String errorMsg = res.getOpRep().getErrMsg();
				checkOrderResult.setErrMsg(res.getOpRep().getRetCode() + " - "+ errorMsg);
				logger.error("南京银行B2C订单" + bankOrder.getOrderId() + "的交易"+ errorMsg);
			}
			//不处理
			else {
				String errorMsg = res.getOpRep().getErrMsg();
				checkOrderResult.setErrMsg(res.getOpRep().getRetCode() + " - "+ errorMsg);
				logger.error("南京银行B2C订单" + bankOrder.getOrderId() + "的交易"+ errorMsg);
			}
			
			return checkOrderResult;
			
		} catch (Exception e) {
			logger.error("bankClient fail.", e);
			checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
			checkOrderResult.setErrMsg("银行对账异常");
			return checkOrderResult;
		}
	}
	@Override
	public BankOrder getDbOrder() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public BaseMessage dayend(String bank_bus) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<CheckOrderResult> checkDayend(String bank_bus) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Result getDayendMsg(String string) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public CheckOrderResult checkDayendOrder(BaseDayendEntry bdm, BankOrder bo) {
		// TODO Auto-generated method stub
		return null;
	}
}
